/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.Identificador;
/**
 *
 * @author Administrador
 */
public class AdminIdentificador implements IdentificadorManager{
    private Connection c;

    public AdminIdentificador(Connection c){
        this.c = c;
    }
    
    @Override
    public Identificador getIdentificador(int gestion) throws DataException{
        Identificador identificador = new Identificador();
        ResultSet rs = null;
        PreparedStatement preSt = null;
        String query = "";
        try{
            query = "SELECT COD_SIS";
            query += " FROM IDENTIFICADOR";
            query += " WHERE GESTION = "+gestion;            
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            if(rs.next()){
                identificador.setGestion(gestion);
                identificador.setCodSis(rs.getInt("COD_SIS"));
            }
        }catch(SQLException e){
            throw new DataException("Error! AdminIdentificador -> getIdentificador(int gestion) e = "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        return identificador;
    }
    
    @Override
    public void updateIdentificadorVentaDePasaje(int gestion, int codSis)throws DataException{
        PreparedStatement preSt = null;
        String query = "";
        try{
            query = "UPDATE IDENTIFICADOR SET COD_SIS = "+codSis;
            query += " WHERE GESTION = "+gestion;            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException e){
            throw new DataException(" AdminIdentificador -> updateIdentificador(int gestion, int codSis) e = "+e.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
}
